package com.gitee.wsl.jvm.reflect.autobox


import com.gitee.wsl.jvm.reflect.ReflectionUtils.cast
import java.math.BigDecimal

class DoubleTypeAutoboxer : TypeAutoboxer<Double> {
    override fun supports(type: Class<*>): Boolean {
        return Double::class.javaPrimitiveType == type || Double::class.java == type
    }

    override fun autobox(value: Any, type: Class<Double>): Double {
        if (value is Double) {
            return cast(value)
        } else if (value is BigDecimal) {
            return cast(value.toDouble())
        } else if (value is Int) {
            return value.toDouble()
        }
        throw UnsupportedOperationException(
            String.format(
                "Cannot autobox %s of type %s to %s",
                value,
                value.javaClass.name,
                Double::class.java.name
            )
        )
    }
}
